package com.teach_sys.dao;

import com.teach_sys.bean.Student;

import java.util.List;

public class StudentDao extends BaseDao {
    /**
     * 向数据库student中添加一条记录
     *
     * @param student 要添加的数据
     * @return 新增的行数，同BaseDao
     */
    public int addStudent(Student student) {
        String sql = "insert into student(Sno,Sname,Ssex,Sage,Sdept) values(?,?,?,?,?)";
        return update(sql, student.getSno(), student.getSname(), student.getSsex(), student.getSage(), student.getSdept());
    }

    /**
     * 向数据库student中删除一条记录
     *
     * @param sno 要删除的记录的Sno
     * @return 删除的行数，同BaseDao
     */
    public int deleteStudentBySno(String sno) {
        String sql = "delete from student where Sno = ?";
        return update(sql, sno);
    }

    /**
     * 更新数据库student中一条记录
     *
     * @param student 要更新的数据对象
     * @return 更新的行数，同BaseDao
     */
    public int updateStudent(Student student) {
        String sql = "update student set Sname=?,Ssex=?,Sage=?,Sdept=? where Sno=?";
        return update(sql, student.getSname(), student.getSsex(), student.getSage(), student.getSdept(), student.getSno());
    }

    /**
     * 通过Sno查询一条记录
     *
     * @param sno Sno
     * @return 查询结果
     */
    public List<Student> queryStudentBySno(String sno) {
        String sql = "select * from student where Sno=?";
        return queryForList(Student.class, sql, sno);
    }

    /**
     * 通过Sname查询记录
     *
     * @param sname Sname
     * @return 查询结果
     */
    public List<Student> queryStudentBySname(String sname) {
        String sql = "select * from student where Sname=?";
        return queryForList(Student.class, sql, sname);
    }

    /**
     * 查询全部记录
     *
     * @return 查询结果
     */
    public List<Student> queryAllStudent() {
        String sql = "select * from student";
        return queryForList(Student.class, sql);
    }
}
